﻿#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "MainTreeWidget.h"
#include "RenderWidget.h"
#include "ActionEventHandler.h"
#include "GUIWidget/ToolBarAnimation.h"
#include "GUIWidget/ToolBarColorMapper.h"
#include "GUIWidget/InformationWidget.h"
#include "GUIWidget/PropertyWidget.h"
#include "GUIWidget/ToolBarFilter.h"
#include "GUIWidget/ToolBarEvent.h"

#include "FITK_Component/FITKWidget/FITKTabWidget.h"

#include <QComboBox>
#include <QDebug>
#include <QLabel>
#include <QSpinBox>
#include <QToolBar>

namespace GUI
{
    MainWindow::MainWindow(QWidget * parent):
        QMainWindow(parent)
    {
        _ui = new Ui::MainWindow();
        _ui->setupUi(this);

        init();
    }

    MainWindow::~MainWindow()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void MainWindow::init()
    {
        setWindowTitle("CFDPost");
        _actionHandler = new ActionEventHandler;
        initCentralWidget();
        initAction();

        _eventWidget = new Comp::FITKTabWidget(Comp::FITKTabWidgetType::FITKTab_None, this);
        _ui->verticalLayout_Event->addWidget(_eventWidget);
        _eventWidget->hide();

        QList<QAction*> actionList = this->findChildren<QAction*>();
        for (QAction* action : actionList) {
            if (action == nullptr)continue;
            connect(action, SIGNAL(triggered()), _actionHandler, SLOT(execOperator()));
        }
    }

    QAction* MainWindow::createAction(QString actionName, QString iconPath, QString actionTitle)
    {
        if (actionName.isEmpty())return nullptr;
        QAction* action = new QAction(this);
        action->setObjectName(actionName);
        action->setIcon(QIcon(iconPath));
        action->setToolTip(actionTitle);
        return action;
    }

    QAction * MainWindow::findAction(QString actionName)
    {
        return this->findChild<QAction*>(actionName);
    }

    RenderWidget * MainWindow::getRenderWidget()
    {
        return _renderWidget;
    }

    MainTreeWidget * MainWindow::getTreeWidget()
    {
        return _treeWidget;
    }

    PropertyWidget * MainWindow::getPropertyWidget()
    {
        return _propertyWidget;
    }

    ToolBarColorMapper * MainWindow::getToolBarColorMapper()
    {
        return _colorMapToolBar;
    }

    ToolBarAnimation* MainWindow::getToolBarAnimation()
    {
        return _animationToolBar;
    }

    ToolBarFilter* MainWindow::getToolFilter()
    {
        return _filterBar;
    }

    ToolBarEvent * MainWindow::getToolEvent()
    {
        return _filterEvent;
    }

    InformationWidget * MainWindow::getInfoWidget()
    {
        return _infoWidget;
    }

    Comp::FITKTabWidget * MainWindow::getEventWidget()
    {
        return _eventWidget;
    }

    void MainWindow::closeEvent(QCloseEvent * event)
    {
        emit this->sigMainWindowClose();
        QMainWindow::closeEvent(event);
    }

    void MainWindow::initCentralWidget()
    {
        _treeWidget = new MainTreeWidget(this);
        _renderWidget = new RenderWidget(this);
        _infoWidget = new GUI::InformationWidget(this);
        _propertyWidget = new GUI::PropertyWidget(this);

        _ui->treeLayout->addWidget(_treeWidget);
        _ui->propertyLayout->addWidget(_propertyWidget);
        _ui->verticalLayout_Render->addWidget(_renderWidget);
        _ui->infoLayout->addWidget(_infoWidget);
    }

    void MainWindow::initAction()
    {
        QAction* action = nullptr;
        QToolBar* tooBar = new QToolBar(this);
        addToolBar(tooBar);
        action = createAction("actionOpen", "://icons/open.png", QString(tr("open")));
        tooBar->addAction(action);
        action = findAction("actionSaveImage");
        if (action)tooBar->addAction(action);
        action = createAction("actionVideo", "://icons/video.png", QString(tr("open")));
        tooBar->addAction(action);
        
        QToolBar* viewBar = new QToolBar(this);
        addToolBar(viewBar);
        action = findAction("actionViewAuto");
        if (action) viewBar->addAction(action);
        action = findAction("actionViewFront");
        if (action) viewBar->addAction(action);
        action = findAction("actionViewBack");
        if (action) viewBar->addAction(action);
        action = findAction("actionViewTop");
        if (action) viewBar->addAction(action);
        action = findAction("actionViewBottom");
        if (action) viewBar->addAction(action);
        action = findAction("actionViewLeft");
        if (action) viewBar->addAction(action);
        action = findAction("actionViewRight");
        if (action) viewBar->addAction(action);
        action = findAction("actionViewIso");
        if (action) viewBar->addAction(action);
        viewBar->addSeparator();

        _animationToolBar = new ToolBarAnimation(this);
        this->addToolBar(_animationToolBar);

        this->addToolBarBreak(Qt::TopToolBarArea);
        _colorMapToolBar = new ToolBarColorMapper(this);
        this->addToolBar(_colorMapToolBar);

        _filterBar = new ToolBarFilter(this);
        this->addToolBar(_filterBar);

        _filterEvent = new ToolBarEvent(this);
        this->addToolBar(_filterEvent);
    }
}

